#![allow(dead_code)]
use serde::Deserialize;
use std::sync::LazyLock;
use crate::config::token::Satoken;

#[cfg(feature = "redis")]
mod redis;
mod token;

static TOKEN_CONFIG: LazyLock<Config> = LazyLock::new(|| Config::load());

#[derive(Debug, Deserialize)]
pub struct Config {
    #[cfg(feature = "redis")]
    redis: redis::RedisConfig,
    #[serde(rename = "token")]
    satoken: Satoken,
}

impl Config {
    pub fn load() -> Self {
        config::Config::builder()
            .add_source(
                config::File::with_name("satoken.yaml")
                    .format(config::FileFormat::Yaml)
                    .required(true)
            )
            .add_source(
                config::Environment::with_prefix("SATOKEN")
                    .try_parsing(true)
                    .separator("_")
                    .list_separator(",")
            ).build().expect("Failed to create Config")
            .try_deserialize().expect("Failed to pase Config")
    }

    #[cfg(feature = "redis")]
    pub fn redis(&self) -> &redis::RedisConfig {
        &self.redis
    }

    pub fn satoken(&self) -> &Satoken { &self.satoken }
}

pub fn get() -> &'static Config {
    &TOKEN_CONFIG
}